package com.wangfei.lessondemo;

import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, NavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    private ActionBarDrawerToggle toggle;
    private ViewPager pager;
    private TabLayout tablayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        navigationView = (NavigationView) findViewById(R.id.slidding_meau);
        navigationView.setNavigationItemSelectedListener(this);

        //设置三条线，显示home
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //三条横线的menu
        toggle = new ActionBarDrawerToggle(this, drawerLayout, 0, 0);
        toggle.syncState();
        //让drawer影响上面的按钮，自带旋转动画
        drawerLayout.setDrawerListener(toggle);

        pager = (ViewPager) findViewById(R.id.viewPager);
        List<String > list=new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add(String.format("第%02d页",i));
        }
        pager.setAdapter(new MyAdapter(getSupportFragmentManager(),list));

        tablayout = (TabLayout) findViewById(R.id.tablayout);
        //一定是setAdapter之后调用
        tablayout.setupWithViewPager(pager);
    }

    //监听mean的菜单控制侧滑菜单
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //Toggle控制Toggle
        if(toggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.slidding_meau:
                //关闭哪一测的滑出菜单
                drawerLayout.closeDrawer(GravityCompat.START);
                startActivity(new Intent(this,MenuActivity.class));
                break;
        }
    }

    //导航条的监听
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        switch (item.getItemId()){

            case  R.id.item_1:
                Toast.makeText(this,"第一项",Toast.LENGTH_SHORT).show();
                break;
            case  R.id.item_2:
                break;
            case  R.id.item_3:
                break;
            case  R.id.item_4:
                ActivityCompat.finishAffinity(this);
//                finishAffinity();//退出所有的
                break;
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }
}
